Micron Document
🎖️GitЯра🎖️

Node / meshcore / MeshCore / files / src / helpers / bridges / ESPNowBridge.h

Displaying Raw • Download

src/helpers/bridges/ESPNowBridge.h 93b2db63de9009af047a2d0e1bcd97b4b0ba7106 (93b2db63) Text, 4.95 KB

Tff7b72#Tff7b72pragma once

Tff7b72#Tff7b72include T8b949e"MeshCore.h"
Tff7b72#Tff7b72include T8b949e"esp_now.h"
Tff7b72#Tff7b72include T8b949e"helpers/bridges/BridgeBase.h"

Tff7b72#Tff7b72ifdef WITH_ESPNOW_BRIDGE

T8b949e/**
* @brief Bridge implementation using ESP-NOW protocol for packet transport
*
* This bridge enables mesh packet transport over ESP-NOW, a connectionless communication
* protocol provided by Espressif that allows ESP32 devices to communicate directly
* without WiFi router infrastructure.
*
* Features:
* - Broadcast-based communication (all bridges receive all packets)
* - Network isolation using XOR encryption with shared secret
* - Duplicate packet detection using SimpleMeshTables tracking
* - Maximum packet size of 250 bytes (ESP-NOW limitation)
*
* Packet Structure:
* [2 bytes] Magic Header - Used to identify ESPNowBridge packets
* [2 bytes] Fletcher-16 checksum of encrypted payload (calculated over payload only)
* [246 bytes max] Encrypted payload containing the mesh packet
*
* The Fletcher-16 checksum is used to validate packet integrity and detect
* corrupted or tampered packets. It's calculated over the encrypted payload
* and provides a simple but effective way to verify packets are both
* uncorrupted and from the same network (since the checksum is calculated
* after encryption).
*
* Configuration:
* - Define WITH_ESPNOW_BRIDGE to enable this bridge
* - Define _prefs->bridge_secret with a string to set the network encryption key
*
* Network Isolation:
* Multiple independent mesh networks can coexist by using different
* _prefs->bridge_secret values. Packets encrypted with a different key will
* fail the checksum validation and be discarded.
*/
Te6edf3class Te6edf3ESPNowBridge Tff7b72: Te6edf3public Te6edf3BridgeBase Tb4b4b4{
Te6edf3privateTff7b72:
Tff7b72static Te6edf3ESPNowBridge Tff7b72*Te6edf3_instanceTb4b4b4;
Tff7b72static Tffa657void Td2a8ffrecv_cbTb4b4b4(Tff7b72const Tffa657uint8_t Tff7b72*Te6edf3macTb4b4b4, Tff7b72const Tffa657uint8_t Tff7b72*Te6edf3dataTb4b4b4, Tffa657int32_t Te6edf3lenTb4b4b4)Tb4b4b4;
Tff7b72static Tffa657void Td2a8ffsend_cbTb4b4b4(Tff7b72const Tffa657uint8_t Tff7b72*Te6edf3macTb4b4b4, Te6edf3esp_now_send_status_t Te6edf3statusTb4b4b4)Tb4b4b4;

T8b949e/**
* ESP-NOW Protocol Structure:
* - ESP-NOW header: 20 bytes (handled by ESP-NOW protocol)
* - ESP-NOW payload: 250 bytes maximum
* Total ESP-NOW packet: 270 bytes
*
* Our Bridge Packet Structure (must fit in ESP-NOW payload):
* - Magic header: 2 bytes
* - Checksum: 2 bytes
* - Available payload: 246 bytes
*/
Tff7b72static Tff7b72const Tffa657size_t Te6edf3MAX_ESPNOW_PACKET_SIZE Tff7b72= T79c0ff250Tb4b4b4;

T8b949e/**
* Size constants for packet parsing
*/
Tff7b72static Tff7b72const Tffa657size_t Te6edf3MAX_PAYLOAD_SIZE Tff7b72= Te6edf3MAX_ESPNOW_PACKET_SIZE Tff7b72- Tb4b4b4(Te6edf3BRIDGE_MAGIC_SIZE Tff7b72+ Te6edf3BRIDGE_CHECKSUM_SIZETb4b4b4)Tb4b4b4;

T8b949e/** Buffer for receiving ESP-NOW packets */
Tffa657uint8_t Te6edf3_rx_bufferTb4b4b4[Te6edf3MAX_ESPNOW_PACKET_SIZETb4b4b4]Tb4b4b4;

T8b949e/** Current position in receive buffer */
Tffa657size_t Te6edf3_rx_buffer_posTb4b4b4;

T8b949e/**
* Performs XOR encryption/decryption of data
* Used to isolate different mesh networks
*
* Uses _prefs->bridge_secret as the key in a simple XOR operation.
* The same operation is used for both encryption and decryption.
* While not cryptographically secure, it provides basic network isolation.
*
* @param data Pointer to data to encrypt/decrypt
* @param len Length of data in bytes
*/
Tffa657void Td2a8ffxorCryptTb4b4b4(Tffa657uint8_t Tff7b72*Te6edf3dataTb4b4b4, Tffa657size_t Te6edf3lenTb4b4b4)Tb4b4b4;

T8b949e/**
* ESP-NOW receive callback
* Called by ESP-NOW when a packet is received
*
* @param mac Source MAC address
* @param data Received data
* @param len Length of received data
*/
Tffa657void Td2a8ffonDataRecvTb4b4b4(Tff7b72const Tffa657uint8_t Tff7b72*Te6edf3macTb4b4b4, Tff7b72const Tffa657uint8_t Tff7b72*Te6edf3dataTb4b4b4, Tffa657int32_t Te6edf3lenTb4b4b4)Tb4b4b4;

T8b949e/**
* ESP-NOW send callback
* Called by ESP-NOW after a transmission attempt
*
* @param mac_addr Destination MAC address
* @param status Transmission status
*/
Tffa657void Td2a8ffonDataSentTb4b4b4(Tff7b72const Tffa657uint8_t Tff7b72*Te6edf3mac_addrTb4b4b4, Te6edf3esp_now_send_status_t Te6edf3statusTb4b4b4)Tb4b4b4;

Te6edf3publicTff7b72:
T8b949e/**
* Constructs an ESPNowBridge instance
*
* @param prefs Node preferences for configuration settings
* @param mgr PacketManager for allocating and queuing packets
* @param rtc RTCClock for timestamping debug messages
*/
Te6edf3ESPNowBridgeTb4b4b4(Te6edf3NodePrefs Tff7b72*Te6edf3prefsTb4b4b4, Te6edf3meshTff7b72:Tff7b72:Te6edf3PacketManager Tff7b72*Te6edf3mgrTb4b4b4, Te6edf3meshTff7b72:Tff7b72:Te6edf3RTCClock Tff7b72*Te6edf3rtcTb4b4b4)Tb4b4b4;

T8b949e/**
* Initializes the ESP-NOW bridge
*
* - Configures WiFi in station mode
* - Initializes ESP-NOW protocol
* - Registers callbacks
* - Sets up broadcast peer
*/
Tffa657void Td2a8ffbeginTb4b4b4(Tb4b4b4) Te6edf3overrideTb4b4b4;

T8b949e/**
* Stops the ESP-NOW bridge
*
* - Removes broadcast peer
* - Unregisters callbacks
* - Deinitializes ESP-NOW protocol
* - Turns off WiFi to release radio resources
*/
Tffa657void Td2a8ffendTb4b4b4(Tb4b4b4) Te6edf3overrideTb4b4b4;

T8b949e/**
* Main loop handler
* ESP-NOW is callback-based, so this is currently empty
*/
Tffa657void Td2a8ffloopTb4b4b4(Tb4b4b4) Te6edf3overrideTb4b4b4;

T8b949e/**
* Called when a packet is received via ESP-NOW
* Queues the packet for mesh processing if not seen before
*
* @param packet The received mesh packet
*/
Tffa657void Td2a8ffonPacketReceivedTb4b4b4(Te6edf3meshTff7b72:Tff7b72:Te6edf3Packet Tff7b72*Te6edf3packetTb4b4b4) Te6edf3overrideTb4b4b4;

T8b949e/**
* Called when a packet needs to be transmitted via ESP-NOW
* Encrypts and broadcasts the packet if not seen before
*
* @param packet The mesh packet to transmit
*/
Tffa657void Td2a8ffsendPacketTb4b4b4(Te6edf3meshTff7b72:Tff7b72:Te6edf3Packet Tff7b72*Te6edf3packetTb4b4b4) Te6edf3overrideTb4b4b4;
Tb4b4b4}Tb4b4b4;

Tff7b72#Tff7b72endif

Served by rngit 1.5.4 - Generated in 0.03s